<!-- eslint-disable vue/valid-v-slot -->
<template>
  <div class="main-container">
    <TableBody>
      <template #header>
        <TableHeader ref="tableHeaderRef" :show-filter="false">
          <template #search-content>
            <a-form ref="seachForm" :model="form" :style="{ width: '100%' }">
              <a-row :gutter="24">
                <a-col :span="4">
                  <a-form-item field="title" hide-label>
                    <a-input v-model="form.title" placeholder="请输入公告标题" allow-clear />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select v-model="form.type" allow-clear placeholder="请选择公告类型">
                      <a-option :value="0">普通公告 </a-option>
                      <a-option :value="1">重要公告 </a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="type" hide-label>
                    <a-select v-model="form.topping" allow-clear placeholder="请选择公告读取状态">
                      <a-option :value="1">已读 </a-option>
                      <a-option :value="0">未读 </a-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="addTime" hide-label>
                    <a-date-picker
                      style="width: 100%"
                      v-model="form.addTime"
                      show-time
                      placeholder="请选择开始创建时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="4">
                  <a-form-item field="endTime" hide-label>
                    <a-date-picker
                      style="width: 100%"
                      v-model="form.endTime"
                      show-time
                      placeholder="请选择结束创建时间"
                      :time-picker-props="{ defaultValue: '12:00:00' }"
                      format="YYYY-MM-DD HH:mm:ss"
                      allow-clear
                      :shortcuts="[
                        {
                          label: '今日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            ))
                          ),
                        },
                        {
                          label: '昨日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs('23:59:59', 'HH:mm:ss')
                              .subtract(1, 'day')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '七日',
                          value: () => (
                            (form.addTime = $dayjs('00:00:00', 'HH:mm:ss')
                              .subtract(6, 'day')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '本月',
                          value: () => (
                            (form.addTime = $dayjs('01 00:00:00', 'DD HH:mm:ss').format(
                              'YYYY-MM-DD HH:mm:ss'
                            )),
                            (form.endTime = $dayjs().format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                        {
                          label: '上月',
                          value: () => (
                            (form.addTime = $dayjs(`01 00:00:00`, 'DD HH:mm:ss')
                              .subtract(1, 'month')
                              .format('YYYY-MM-DD HH:mm:ss')),
                            (form.endTime = $dayjs()
                              .subtract(1, 'month')
                              .endOf('month')
                              .format('YYYY-MM-DD HH:mm:ss'))
                          ),
                        },
                      ]"
                    />
                  </a-form-item>
                </a-col>
                <!-- <a-col :span="4">
                  <a-form-item field="addReadNumber" hide-label>
                    <a-input-group>
                      <a-input
                        v-model="form.addReadNumber"
                        placeholder="开始查看人数"
                        allow-clear
                      />
                      <a-input
                        v-model="form.endReadNumber"
                        placeholder="结束查看人数"
                        allow-clear
                      />
                    </a-input-group>
                  </a-form-item>
                </a-col> -->
                <a-col :span="4">
                  <a-form-item field="btns" hide-label>
                    <a-space :size="15">
                      <a-button type="primary" html-type="submit" @click="doRefresh(1)"
                        ><template #icon>
                          <icon-search />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>搜索</template></a-button
                      >
                      <a-button @click="reset"
                        ><template #icon>
                          <icon-sync theme="outline" size="14" />
                        </template>
                        <!-- Use the default slot to avoid extra spaces -->
                        <template #default>重置</template></a-button
                      >
                    </a-space>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form>
          </template>
        </TableHeader>
      </template>
      <!-- <template #table-config>
        <a-space :size="15" />
        <a-space :size="15">
          <TableConfig
            @update-border="onUpdateBorder"
            @update-striped="onUpdateStriped"
            @refresh="doRefresh"
          />
        </a-space>
      </template> -->
      <template #default>
        <a-table
          :loading="tableLoading"
          :stripe="(tables.striped as boolean)"
          :data="table.dataList"
          :pagination="false"
          :row-key="rowKey"
          :row-class-name="rowClassNameFun"
          :bordered="{ wrapper: true, cell: tables.bordered }"
          :scroll="{ x: 1500 }"
          :scrollbar="{ type: 'track', outerClass: '', outerStyle: '' }"
        >
          <template #columns>
            <a-table-column
              v-for="item of tableColumns.list"
              :key="item.key"
              align="left"
              :title="(item.title as string)"
              :width="item.width"
              :data-index="(item.key as string)"
              :fixed="item.fixed"
              :ellipsis="true"
              :tooltip="true"
            >
              <template v-if="item.key === 'isRead'" #cell="{ record }">
                <a-tag v-if="record.isRead == false" color="red">未读</a-tag>
                <a-tag v-else-if="record.isRead == true" color="arcoblue">已读</a-tag>
              </template>
              <template v-if="item.key === 'title'" #cell="{ record }">
                <a-tag v-if="record.topping == 1" color="arcoblue" style="margin-right: 3px"
                  >置顶
                </a-tag>
                <a-tag v-if="record.type == 1" color="red">重要</a-tag>
                {{ record.title }}
              </template>
              <template v-else-if="item.key === 'type'" #cell="{ record }">
                <a-tag v-if="record.type == 1" color="red">重要</a-tag>
                <a-tag v-else-if="record.type == 0" color="arcoblue">普通</a-tag>
              </template>

              <template v-else-if="item.key === 'actions'" #cell="{ record }">
                <a-space :size="2">
                  <a-button
                    @click="detailFns(record)"
                    style="padding: 0px 4px"
                    type="text"
                    size="small"
                    >查看</a-button
                  >
                </a-space>
              </template>
            </a-table-column>
          </template>
        </a-table>
      </template>
      <template #footer>
        <!-- <TableFooter :pagination="pagination" /> -->
      </template>
    </TableBody>
    <!-- 查看公告 -->
    <a-modal
      v-model:visible="visibles"
      @before-close="beforeClose"
      :mask-closable="false"
      width="800px"
      title="查看公告"
      title-align="start"
    >
      <div>
        <div
          style="
            display: flex;
            justify-content: space-between;
            padding: 10px;
            background-color: #e8f7ff;
            color: #3491fa;
          "
        >
          <div> 公告发布时间：{{ detail.addTime }} </div>
          <a-tag v-if="detail.type == 1" color="red">重要</a-tag>
          <a-tag v-else-if="detail.type == 0" color="arcoblue">普通</a-tag>
        </div>
        <div style="color: #3491fa; font-size: 16px; text-align: center; padding: 20px">{{
          detail.title
        }}</div>
        <div v-html="detail.content" id="notices" style="padding: 20px"></div>
      </div>
      <template #footer>
        <a-space>
          <!-- <a-button type="primary" @click="refundFn">原路退款</a-button> -->
        </a-space>
      </template>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
  import { getAboutUserNotice, addRead } from '@/api/user'
  import { get } from '@/api/http'
  import { usePagination, useRowKey, useTable } from '@/hooks/table'
  import { onMounted, reactive, ref } from 'vue'
  import { Message } from '@arco-design/web-vue'
  const pagination = usePagination(doRefresh)
  const rowKey = useRowKey('id')
  const table = useTable()
  const visibles = ref(false)
  const seachForm = ref()
  const details = ref()
  const detail: any = ref({})
  const form = reactive({
    title: '',
    type: '',
    topping: '',
    forceOpen: '',
    addTime: '',
    endTime: '',
    addReadNumber: '', // 开始已读人数
    endReadNumber: '', // 结束已读人数
  })
  const tableLoading = ref(false)
  const tables = reactive({
    tableLoading: false,
    bordered: false,
    striped: false,
    expand: [1],
    isExpand: false,
  })
  const tableColumns: any = reactive({
    list: [
      {
        title: '公告标题',
        key: 'title',
        dataIndex: 'title',
        checked: true,
      },
      // {
      //   title: '查看人数',
      //   key: 'systemNoticeReadList',
      //   dataIndex: 'systemNoticeReadList',
      //   checked: true,
      // },
      {
        title: '公告类型',
        key: 'type',
        dataIndex: 'type',
        checked: true,
      },
      {
        title: '读取状态',
        key: 'isRead',
        dataIndex: 'isRead',
        checked: true,
      },
      {
        title: '创建时间',
        key: 'addTime',
        dataIndex: 'addTime',
        checked: true,
        width: 190,
      },
      {
        title: '操作',
        key: 'actions',
        fixed: 'right',
        dataIndex: 'actions',
        width: 120,
        align: 'center',
        checked: true,
      },
    ],
  })

  const reset = () => {
    seachForm.value.resetFields()
    doRefresh(1)
  }

  const beforeClose = () => {
    if (detail.value.isRead == false) {
      get({
        url: addRead,
        data: () => {
          return { noticeId: detail.value.id }
        },
      })
        .then((res) => {
          Message.success(res.msg)
          visibles.value = false
          doRefresh()
        })
        .catch(console.log)
    }
    // details.value.resetFields()
    visibles.value = false
  }

  const detailFns = (val: any) => {
    detail.value = JSON.parse(JSON.stringify(val))
    visibles.value = true
  }

  function doRefresh(value?: number) {
    // eslint-disable-next-line prettier/prettier
    tableLoading.value = true
    if (value) {
      pagination.page = value
    }
    get({
      url: getAboutUserNotice,
      data: () => {
        return {
          page: pagination.page,
          size: pagination.pageSize,
          ...form,
        }
      },
    })
      .then((res) => {
        tableLoading.value = false
        table.handleSuccess(res.data)
        pagination.setTotalSize((res.data as any).totalSize)
      })
      .catch(console.log)
  }
  function rowClassNameFun(_record: any, index: number) {
    return index % 2 === 1 && table.striped.value ? 'table-striped' : null
  }

  const onUpdateBorder = (isBordered: boolean) => {
    tables.bordered = isBordered
  }
  function onUpdateStriped(isStriped: boolean) {
    tables.striped = isStriped
  }

  onMounted(async () => {
    await doRefresh()
  })
</script>

<style lang="less" scoped>
  :deep(#notices ul) {
    list-style: disc !important;
  }
</style>
